#!/bin/bash

# Check for -y flag
auto_remove_image=false
if [[ "$1" == "-y" ]]; then
    auto_remove_image=true
fi

# Stop 1-4 network containers
for i in {1..4}; do
    echo "Stopping container custom-dind-$i"
    docker stop "custom-dind-$i"
    if [ $? -ne 0 ]; then
        echo "Failed to stop custom-dind-$i"
    else
        echo "custom-dind-$i stopped successfully"
    fi
done

# Remove 1-4 network containers
for i in {1..4}; do
    echo "Removing container custom-dind-$i"
    docker rm "custom-dind-$i"
    if [ $? -ne 0 ]; then
        echo "Failed to remove custom-dind-$i"
    else
        echo "custom-dind-$i removed successfully"
    fi
done

# Choose whether to remove the custom-dind image
if [ "$auto_remove_image" == true ]; then
    remove_image="y"
else
    echo "Do you want to remove the custom-dind image? (y/n)"
    read -r remove_image
fi

if [ "$remove_image" == "y" ]; then
    echo "Removing custom-dind image"
    docker rmi custom-dind:latest
    if [ $? -ne 0 ]; then
        echo "Failed to remove custom-dind image"
    else
        echo "custom-dind image removed successfully"
    fi
else
    echo "Custom-dind image not removed"
fi
